<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col v-if="form.user.userId !== undefined" :span="12">
          <el-form-item :label="$t('用户名称')" prop="user.userId">
            <UserSelect
              v-model="form.user.userId"
              :placeholder="$t('用户名称')"
              :disabled="form.id !== undefined"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('参与角色')" prop="role">
            <el-select v-model="form.role" :placeholder="$t('参与角色')">
              <el-option
                v-for="dict in roleOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('参与时间')" prop="joinTime">
            <DatePicker v-model="form.joinTime" :placeholder="$t('参与时间')" @change="checkTime" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否离开')">
            <el-radio-group v-model="form.leave" @change="change">
              <el-radio
                v-for="dict in leaveOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="isShowLeaveTime" :span="12">
          <el-form-item :label="$t('离开时间')" prop="leaveTime">
            <DatePicker v-model="form.leaveTime" :placeholder="$t('离开时间')" @change="checkTime" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :maxlength="2000"
              show-word-limit
              :rows="2"
              :placeholder="$t('备注')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="!btnHide" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="deal:team" />
    </div>
  </div>
</template>

<script>
import { saveDealTeam, getDealTeam, replaceProjectLeader } from '@/api/deal/team/dealTeam'
    export default {
      props: {
        dealId: {
          type: String,
          default: undefined
        }
      },
        data() {
            return {
                // 参与角色 字典deal_team_role字典
                roleOptions: [], // 是否离开 字典 sys_yes_no字典
                leaveOptions: [], // 表单参数
                form: {
                  user: {
                    userId: ''
                  }
                },
                // 是否显示离开时间 默认false
                isShowLeaveTime: false,
                // 遮罩层
                loading: false,
                // 是否隐藏保存等按钮
                btnHide: false,
                // 表单校验
                rules: {
                  'user.userId': [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ],
                  'role': [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ],
                  'joinTime': [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ]
                }
            }
        },
        created() {
            // this.form.leave = '否'
            // this.$set(this.form, 'dealId', this.$route.query.id)
            this.getDicts('deal_team_role').then(response => {
                this.roleOptions = response.data
            })
            this.getDicts('sys_yes_no').then(response => {
                this.leaveOptions = response.data
            })
            this.$bus.$emit('async_done')
        },
      methods: {
            // 初始化数据
            init(data) {
              this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealTeam(data.id).then(response => {
                        this.form = response.data
                        this.change(this.form.leave)
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.change(this.form.leave)
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                user: {
                  userId: ''
                },
                dealId: this.$route.query.id ? this.$route.query.id : this.dealId,
                role: undefined,
                joinTime: new Date(),
                leave: 'N',
                leaveTime: undefined,
                remark: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealTeam(this.form).then(response => {
                            if (response.code === 200 && response.data !== null) {
                                if (response.data.code === 200) {
                                  this.msgSuccess(this.$t(response.data.msg))
                                  this.closeForm()
                                } else if (response.data.code === 10001) {
                                  this.replaceProjectLeader(response.data.data, response.data.msg)
                                } else {
                                  this.msgError(this.$t(response.data.msg))
                                }
                                this.loading = false
                            } else {
                                this.msgError(response.msg)
                                this.reset()
                                this.loading = false
                            }
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          // 替换项目负责人
          replaceProjectLeader(data, msg) {
            var _this = this
            _this.$nextTick(() => {
              _this.$confirm(_this.$t(msg), _this.$t('提示'), {
                confirmButtonText: _this.$t('确定'),
                cancelButtonText: _this.$t('取消'),
                type: 'warning'
              }).then(() => {
                this.form.oldId = data.id
                this.form.userId = this.form.user.userId
                this.loading = true
                replaceProjectLeader(this.form).then(response => {
                  console.log('替换项目负责人', response)
                  this.closeForm()
                })
              }).catch(() => {
              })
            })
          },
          change(value) {
            // eslint-disable-next-line no-empty
            if (value === 'Y') {
              this.isShowLeaveTime = true
            } else {
              this.isShowLeaveTime = false
              this.form.leaveTime = undefined
            }
          },
          checkTime() {
            var start = this.form.joinTime
            var end = this.form.leaveTime
            if (start && end) {
              if (new Date(start).getTime() >= new Date(end).getTime()) {
                this.form.leaveTime = undefined
                this.msgError(this.$t('不能早于参与时间'))
              }
            }
          }

        }

    }
</script>
